﻿Imports ISNet.WebUI.WebGrid
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports log4net


Partial Class mtUsersFCenter
    Inherits System.Web.UI.Page

    Public AlertText As String

    Private Shared logger As ILog = LogManager.GetLogger("mtUsersFCenter")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        ScreenName.Text = "ระบบจัดทำงบประมาณ  >>  ตั้งค่าระบบ >>  Users - FundCenter"

        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If
        
        If IsPostBack = False Then
            WebGrid1.LayoutSettings.AllowAddNew = AddNew.No
        End If
        If IsPostBack = False Then
            PlanYearsTxt.Text = Session("PlanYear")
            'Session("PlanYearTxt") = PlanYearsTxt.Text
        End If


    End Sub

    Protected Sub WebGrid1_InitializeDataSource(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.DataSourceEventArgs) Handles WebGrid1.InitializeDataSource
        Dim adapter As New MasterDataSetTableAdapters.UserFundCentersTableAdapter
        Dim dtTable As New MasterDataSet.UserFundCentersDataTable

        Dim sql As String = "select users.UserName, UserFundCenters.* from UserFundCenters join users  on users.UserName=UserFundCenters.User_UserName "
        sql += " WHERE 1 = 1 "

        If UsersWebCombo.Value = "" Then
            ' sql += " and users.FundCenter_PlanYear = " & PlanYearsTxt.Text
            sql += " and UserFundCenters.PlanYear = " & PlanYearsTxt.Text
        Else

            'If UsersWebCombo.Value = "" Then
            sql += " and users.UserName  = '" & UsersWebCombo.Value & "'"
            'ElseIf SearchTxt.Text = "" And UsersWebCombo.Value = "" Then
            '    sql += " and users.UserName  = '" & SearchTxt.Text & "'"
            'ElseIf UsersWebCombo.Value <> "" And SearchTxt.Text = "" Then
            '    sql += " and users.UserName = '" & UsersWebCombo.Value & "'"
            'ElseIf SearchTxt.Text <> "" And UsersWebCombo.Value <> "" Then
            '    sql += " and users.UserName = '" & SearchTxt.Text & "'"
            'End If
            ' sql += " and users.FundCenter_PlanYear = " & PlanYearsTxt.Text
            sql += " and UserFundCenters.PlanYear = " & PlanYearsTxt.Text

        End If

        sql += " ORDER BY FromFundCenter_FundCenterCode "

        adapter.SearchUsersFCenter(dtTable, sql)
        'Response.Write(sql)
        Session("count_data") = dtTable.Rows.Count
        dtTable.Columns("ToFundCenter_PlanYear").AllowDBNull = True
        dtTable.Columns("ToFundCenter_FundCenterCode").AllowDBNull = True
        dtTable.Columns("FromFundCenter_PlanYear").AllowDBNull = True
        dtTable.Columns("FromFundCenter_FundCenterCode").AllowDBNull = True
        dtTable.Columns("User_UserName").AllowDBNull = True
        dtTable.Columns("PlanYear").AllowDBNull = True

        dtTable.Columns("Id").ReadOnly = False

        ' dtTable.Columns("PlanYear").AllowDBNull = True
        'dtTable.Columns("FundCenterCode").AllowDBNull = True
        e.DataSource = dtTable
        'If dtTable.Count = 0 Then
        '    WebGrid1.LayoutSettings.AllowAddNew = AddNew.No
        'Else
        '    WebGrid1.LayoutSettings.AllowAddNew = AddNew.Yes
        'End If
    End Sub

    Protected Sub WebGrid1_InitializeRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        ' don't edit column
        e.Row.Cells(1).ForceNoEdit = True
        e.Row.Cells(4).ForceNoEdit = True
        e.Row.Cells(1).Text = e.Row.DataSourceIndex + 1
    End Sub

    Protected Sub WebGrid1_AddRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        Dim adapter As New MasterDataSetTableAdapters.UserFundCentersTableAdapter
        Dim check = adapter.CheckExistUsersFC(Session("PlanYear"), UsersWebCombo.Value, e.Row.Cells(2).Value, e.Row.Cells(3).Value)

        Dim id As Decimal
        If check <> 0 Then
            WebGrid1.ClearCachedDataSource()
            Throw New ArgumentException("ข้อมูลผู้ใช้งานมี Fund Center ซ้ำกับที่มีอยู่ กรุณาตรวจสอบ")
            WebGrid1.ClientAction.Refresh()
        Else
            If Val(e.Row.Cells(3).Value) < Val(e.Row.Cells(2).Value) Then
                WebGrid1.ClearCachedDataSource()
                Throw New ArgumentException("ข้อมูลถึง Fund Center น้อยกว่า จาก กรุณาตรวจสอบ")
            End If

            id = adapter.InsertUsersFCtr(Val(Session("PlanYear")), Date.Now, Session("users"), UsersWebCombo.Value, e.Row.Cells(2).Value, Val(Session("PlanYear")), e.Row.Cells(3).Value, Val(Session("PlanYear")))
            logger.Info("User " & Session("users") & " insert user-fundcenter with user " & UsersWebCombo.Value & " and fund center " & e.Row.Cells(2).Value & "-" & e.Row.Cells(3).Value)
        End If

        Dim dr As DataRow = e.Row.DataRow
        dr("Id") = id
    End Sub

    Protected Sub WebGrid1_UpdateRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        Dim id As String
        id = e.Row.KeyValue
        Dim adapter As New MasterDataSetTableAdapters.UserFundCentersTableAdapter

        Dim check = adapter.CheckExistUpdate(Session("PlanYear"), UsersWebCombo.Value, e.Row.Cells(2).Value, e.Row.Cells(3).Value, id)
        If check <> 0 Then
            WebGrid1.ClearCachedDataSource()
            WebGrid1.ClientAction.Refresh()
            Throw New ArgumentException("ข้อมูลผู้ใช้งานมี Fund Center ซ้ำกับที่มีอยู่ กรุณาตรวจสอบ")
        Else
            If UsersWebCombo.Value = "" Then
                WebGrid1.ClientAction.Refresh()
                WebGrid1.ClientAction.Alert("ไม่สามารถแก้ไขข้อมูลได้ กรุณาเลือก User Name")
            Else
                adapter.UpdateUsersFCtr(Val(Session("PlanYear")), Date.Now, Session("users"), UsersWebCombo.Value, e.Row.Cells(2).Value, Val(Session("PlanYear")), e.Row.Cells(3).Value, Val(Session("PlanYear")), id)
                logger.Info("User " & Session("users") & " update user-fundcenter with user " & UsersWebCombo.Value & " and fund center " & e.Row.Cells(2).Value & "-" & e.Row.Cells(3).Value)
            End If

        End If
    End Sub

    Protected Sub WebGrid1_InitializePostBack(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.PostbackEventArgs)
        If e.Action = PostBackAction.Custom Then
            Dim type As String = TryCast(Request("type"), String)
            Dim Deletet As String = TryCast(Request("id"), String)
            Dim adapter As New MasterDataSetTableAdapters.UserFundCentersTableAdapter
            If type = "1" Then
                ' MsgBox(Deletet)
                adapter.DeleteUsersFCtr(Deletet)
            ElseIf type = "2" Then
                For Each keyValue As String In WebGrid1.RootTable.GetCheckedRows()
                    adapter.DeleteUsersFCtr(keyValue)
                Next keyValue
            End If
            WebGrid1.ClientAction.Refresh()
        End If

    End Sub
    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click
        If UsersWebCombo.Value = "" Then
            Alert("กรุณาเลือกชื่อผู้ใช้งาน")
            WebGrid1.LayoutSettings.AllowAddNew = AddNew.No
            Exit Sub
        Else
            WebGrid1.LayoutSettings.AllowAddNew = AddNew.Yes
        End If

        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
        t_countdata.Visible = True
        If Val(Session("count_data")) > 0 Then
            L_CountData.Text = FormatNumber(Val(Session("count_data")), 0) & " รายการ"
        Else
            L_CountData.Text = "ไม่พบข้อมูลที่ค้นหา"
        End If
    End Sub
    Protected Sub FundCenterCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles FundCenterCombo.InitializeDataSource
        Dim adapter As New MasterDataSetTableAdapters.FundCenters2TableAdapter
        Dim dt As New MasterDataSet.FundCenters2DataTable
        dt = adapter.GetDataByPlanYear(Val(PlanYearsTxt.Text))

        e.DataSource = dt
    End Sub
    Protected Sub FundCenterCombo2_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles FundCenterCombo2.InitializeDataSource
        Dim adapter As New MasterDataSetTableAdapters.FundCenters2TableAdapter
        Dim dt As New MasterDataSet.FundCenters2DataTable
        dt = adapter.GetDataByPlanYear(Val(PlanYearsTxt.Text))

        e.DataSource = dt
    End Sub

    Protected Sub UsersWebCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles UsersWebCombo.InitializeDataSource

        Dim adap As New MasterDataSetTableAdapters.Users2TableAdapter
        Dim dt As New MasterDataSet.Users2DataTable
        ' dt = adap.GetDataByPlanYear(Val(PlanYearsTxt.Text))
        dt = adap.GetDataByPlanYear()
        e.DataSource = dt

    End Sub
    Sub Alert(ByVal message As String)
        AlertText = "<script> alert('" & message & "');</script>"
    End Sub

    Protected Sub SaveBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SaveBtn.Click
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
    End Sub

End Class
